/*
 * Todoit 'Text-Add' command-bar.
 */  
	function goCli() {
		var cli = $('#cli').val();

		processCliTask(cli);

		$('#cli').val('');
	}

	function processCliTask(cli) {
		if (jQuery.trim(cli).length == 0)
			return null;

		// Debug cheat codes, prefixed with ']':
		// delete task id prefixed by ']t]'. e.g. ']t]42' deletes task 42
		// debug: ']<key>' deletes <key>
		//        ']]]' deletes all keys
		if (showDebug && cli.indexOf(']') == 0) {
			var id = cli.substr(1);

			if (id == ']]') {
				log(']]]deleting all keys!!!');
				firstrender = true;
				wave.getState().reset();
				all_tasks = [];
				all_tasks_vers = [];
			}	else if(id.indexOf('t]') == 0) {
				id = id.substr(2);
				log(']t]deleting task id: task' + id);
				deleteTasks([{'id': 'task'+id}]);
			} else {
				log(']deleting key: ' + id);
				setVal(id, null);
			}

		}

		else {
			log('try adding task: ' + cli);

			// make task object
			var t = textToTask(cli);

			if(t) {
				addTask(t);

				//$('#cli').css('color', 'blue');
			}
		}

	}

	// convert text with special hinting ("^*=#!@") to Task object
	function textToTask(txt) {
		var reg = new RegExp('([\\^*=#!@])', 'g');
		var urlregex = new RegExp('(f|ht)tps?:\\/\\/\\S+\\s?','g');

		var ar = txt.split(reg);
		var len = ar.length - 1;
		var last = 0;
		var i,j = 0;

		logm(ar);

		var t = new task();
		t.title = jQuery.trim(ar[0]);
		if (t.title.length < 1) 
			return;
		t.id = null; // to be set at addTask()

		t.link = txt.match(urlregex);
		if (t.link != null) {
			t.link = jQuery.trim(t.link[0]);
		}

		for (i = 1; i <= len; i++) {
			if (i == len) last = 1;

			cur = jQuery.trim(ar[i]);

			if (!last) {
				var next = jQuery.trim(ar[i+1]);

				// TODO: VALIDATION!
				if (cur == '^') {
					var vtok = validDate(next);
					if (vtok) t.dueDate = vtok;
					i++;
				}
				else if (cur == '*') {
					var vtok = validFrequency(next);
					if (vtok) t.freq = vtok;
					i++;
				}
				else if (cur == '=') {
					var vtok = validDuration(next);
					if (vtok) t.dur = vtok;
					i++;
				}
				else if (cur == '#') {
					(t.tags).push(next);
					i++;
				}
				else if (cur == '!') {
					var vtok = validPriority(next);
					if (vtok) t.priority = vtok;
					i++;
				}
				else if (cur == '@') {
					t.location = next;
					i++;
				}
			}
		}

		// TODO: auto-recognize common tokens
		if (!t.dueDate) {
			var dregex = new RegExp('(today|tomorrow)','g');
			var m = t.title.match(dregex);
			if (m) t.dueDate = Date.parse(m[0]);
		}

		logm(t);
		logm(JSON.stringify(t));

		return t;
	}
	
/*
	function txtToFreq(txt, from) {
		if (!txt || txt.length == 0) return [];
		var due = from ? from.clone() : Date.today();
		var p = Date.parse(txt);
		if (txt.indexOf('after') == 0) {
			var d = (p - Date.today());
			p = due.addMilliseconds(d);
		}
		if (p) return [1,p]; // only one other time

		// TODO: handle freq repeats when completeTask()ing
		return ['r',txt]; // every * time (e.g. 'daily')
	}
*/

	function initCli() {

		$("#cli").autocomplete(ac_data, {
				multiple: true,
				multipleSeparator: " ",
				formatItem: function(d,i,n,g,y) {
					var t = new Date().getDay();
					if (d[0] == ac_days[t] || d[0] == ac_days[(t+1)%7])
						return false;
					return y.length > 1 ? d[0].replace(new RegExp(
						'('+y.substr(1)+')','i'), '<strong>$1</strong>').substr(1) : 
						d[0].substr(1); // AC is buggy
				},
				formatMatch: function(r){return r[0]},
				formatResult: function(r){return r[0]},
				max: 9,
				width: 'auto'
			});

			// TODO: mod AC.js to set css(left += caret pixel position) ?

		//$('#cli').blur(function() {$(this).css('background-color', '#f5f5ff')});
		//$('#cli').focus(function() {$(this).css('background-color', '#fff')});
		/*$('#cli-ichelp').toggle(function () {
				var o = $('#cli-ichelp').offset();
				$('#cli-helptip').css({'left': o.left+11,
					'top': o.top+9}).fadeIn(200);
			}, function() { $('#cli-helptip').fadeOut(200); });*/
	}
